import java.util.concurrent.atomic.AtomicInteger;

public class ThreadDemo21 {
    static class Counter{
        public AtomicInteger count=new AtomicInteger(0);

        public void increse() {
            count.getAndIncrement();//完成 ++，不需要加锁，原子类内部通过 CAS保证线程安全
        }
    }



    public static void main(String[] args) {
        Counter counter = new Counter();

        Thread t1=new Thread() {
            @Override
            public void run() {
                for (int i = 0; i <5_0000; i++) {
                    counter.increse();
                }
            }
        };

        Thread t2=new Thread() {
            @Override
            public void run() {
                for (int i = 0; i <5_0000; i++) {
                    counter.increse();
                }
            }
        };

        t1.start();
        t2.start();

        try {
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(counter.count);

    }
}
